import { defineStore } from "pinia";
import { ref } from "vue";
import { parse, stringify } from "zipson";

export const useCounterStore = defineStore(
	"counter",
	() => {
		// 定义状态
		const count = ref(0);
		const saveMeToo = ref(0);
		const handleAdd = () => {
			count.value++;
			saveMeToo.value++;
		};
		return {
			count,
			saveMeToo,
			handleAdd,
		};
	},
	{
		persist: {
			key: "my-custom-key", // 存储的键名
			storage: sessionStorage, // 存储方式，可以是 sessionStorage 或 localStorage
			paths: ["count"], // 该 store 中, 哪些变量会被持久化。
			// 处理序列化和返序列化
			serializer: {
				serialize: stringify,
				deserialize: parse,
			},
		},
	},
);
